#!/bin/bash

arch=$1

rm -rf ./sqfsroot-tmp
mkdir ./sqfsroot-tmp
rm -rf ./base_install_list
while read line
do
    echo -n $line >> base_install_list
    echo -e -n "\t" >> base_install_list
done < "base_rpmlist_$arch"

dnf --installroot=ROOTPATH --disablerepo="*" URL \
--nogpgcheck --releasever=1 -y install dnf NetworkManager --setopt=install_weak_deps=False

mount -o bind /dev ROOTPATH/dev
mount -o bind /run ROOTPATH/run
mount -t devpts devpts ROOTPATH/dev/pts
mount -t proc proc ROOTPATH/proc
mount -t sysfs sysfs ROOTPATH/sys

cat /etc/resolv.conf > ROOTPATH/etc/resolv.conf

chroot ROOTPATH dnf --setopt=install_weak_deps=False -y --nogpgcheck --releasever=1 --disablerepo="*" URL install $(cat base_install_list)

sync && sleep 1

kern_ver=`ls ROOTPATH/lib/modules`
if [ ! -z "$kern_ver" ]; then
    chroot ROOTPATH dracut -f --add-drivers 'virtio_blk virtio_net xfs' /boot/initramfs-${kern_ver}.img --kver ${kern_ver}
fi

echo -n > ROOTPATH/etc/resolv.conf

umount ROOTPATH/dev/pts
umount ROOTPATH/dev
umount ROOTPATH/run
umount ROOTPATH/proc
umount ROOTPATH/sys
